<?php
class Install_model extends CI_Model{

	function is_installed(){
		if($this->db->table_exists('account')){
			return true;
		}else{
			return false;
		}
		
		
	}
	
	function get_table_info(){
		
//		$db_name = "u338797309_cfrenzy.sql";
$data[] = "SET FOREIGN_KEY_CHECKS=0;";	
		$data[] = "CREATE TABLE `account` (
  `account_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(8) NOT NULL,
  `password` varchar(32) NOT NULL,
  `active` int(11) NOT NULL,
  PRIMARY KEY (`account_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;";
		
		$data[] = "CREATE TABLE `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(255) NOT NULL,
  `category_description` varchar(255) NOT NULL,
  `category_parent` int(11) NOT NULL,
  `category_basename` varchar(255) NOT NULL,
  PRIMARY KEY (`category_id`),
  UNIQUE KEY `category_basename` (`category_basename`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;";
		
		$data[] = "CREATE TABLE `entry` (
  `entry_id` int(11) NOT NULL AUTO_INCREMENT,
  `entry_name` varchar(255) NOT NULL,
  `entry_basename` varchar(255) NOT NULL,
  `entry_category_parent` int(11) NOT NULL,
  `entry_content` text NOT NULL,
  `entry_author` varchar(255) NOT NULL,
  `entry_date` date DEFAULT NULL,
  `entry_pubish` int(11) DEFAULT NULL,
  PRIMARY KEY (`entry_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;";
		
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `active_account` AS select `account`.`account_id` AS `account_id`,`account`.`username` AS `username`,`account`.`password` AS `password`,`account`.`active` AS `active` from `account` where (`account`.`active` = 1);";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `all_category` AS select `category`.`category_id` AS `category_id`,`category`.`category_name` AS `category_name`,`category`.`category_description` AS `category_description`,`category`.`category_parent` AS `category_parent`,`category`.`category_basename` AS `category_basename` from `category`;";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `all_entries` AS select `entry`.`entry_id` AS `entry_id`,`entry`.`entry_name` AS `entry_name`,`entry`.`entry_basename` AS `entry_basename`,`entry`.`entry_content` AS `entry_content`,`entry`.`entry_author` AS `entry_author`,`entry`.`entry_date` AS `entry_date`,`entry`.`entry_pubish` AS `entry_publish`,`category`.`category_name` AS `parent_name` from (`entry` join `category` on((`entry`.`entry_category_parent` = `category`.`category_id`)));";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `hasparent` AS select `category`.`category_id` AS `category_id`,`category`.`category_name` AS `category_name`,`category`.`category_description` AS `category_description`,`category`.`category_parent` AS `category_parent`,`category`.`category_basename` AS `category_basename`,`parent`.`category_name` AS `parent_name` from (`category` join `category` `parent` on((`category`.`category_parent` = `parent`.`category_id`))) where (`category`.`category_parent` <> 1);";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `published` AS select `entry`.`entry_id` AS `entry_id`,`entry`.`entry_name` AS `entry_name`,`entry`.`entry_basename` AS `entry_basename`,`entry`.`entry_category_parent` AS `entry_category_parent`,`entry`.`entry_content` AS `entry_content`,`entry`.`entry_author` AS `entry_author`,`entry`.`entry_date` AS `entry_date`,`entry`.`entry_pubish` AS `entry_pubish`,`category`.`category_name` AS `category_name` from (`entry` join `category` on((`entry`.`entry_category_parent` = `category`.`category_id`))) where (`entry`.`entry_pubish` = 1);";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `topcategory` AS select `category`.`category_id` AS `category_id`,`category`.`category_name` AS `category_name`,`category`.`category_description` AS `category_description`,`category`.`category_parent` AS `category_parent`,`category`.`category_basename` AS `category_basename` from `category` where (`category`.`category_parent` = 1);";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `unpublished` AS select `entry`.`entry_id` AS `entry_id`,`entry`.`entry_name` AS `entry_name`,`entry`.`entry_basename` AS `entry_basename`,`entry`.`entry_category_parent` AS `entry_category_parent`,`entry`.`entry_content` AS `entry_content`,`entry`.`entry_author` AS `entry_author`,`entry`.`entry_date` AS `entry_date`,`entry`.`entry_pubish` AS `entry_pubish`,`category`.`category_name` AS `category_name` from (`entry` join `category` on((`entry`.`entry_category_parent` = `category`.`category_id`))) where (`entry`.`entry_pubish` = 0);";
		$data[] = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `valid_category` AS select `category`.`category_id` AS `category_id`,`category`.`category_name` AS `category_name`,`category`.`category_description` AS `category_description`,`category`.`category_parent` AS `category_parent`,`category`.`category_basename` AS `category_basename` from `category` where (`category`.`category_parent` <> 0);";
		return $data;

//read the file

	return $data;
}
	
	function create_table($data){
		if($data){
			foreach ($data as $row){
				$this->db->query($row);
			}
		}
		return true;
	}
	
	function create_admin($data){
		if($this->db->insert('account', $data) == true){
			return true;
		}
		
	}
}